const app = getApp()
const amapFile = require('../../utils/amap-wx.js');
let markersData = {
  latitude: '',//纬度
  longitude: '',//经度
  key: "a5107c07eb68b1168323d196ade0ad6e"//申请的高德地图key
};
Page({
  data: {
    autoplay:true,
    interval:3000,
    duration:500,
    circular:true,
    preIndex: '0',
    bannerList:[],
    appoList:[],
    weizhi:'花都区',
    yue:11,
    money:0,
    compare: app.globalData.min_money,
    navList:[],
    hidden:true,
    latitude:'23.405755',
    longitude:'113.226773'
  },
  address(e){
    let self=this;
    self.setData({
      hidden: !self.data.hidden
    })
 
  },
  focus(e){
    wx.navigateTo({
      url: './search/search',
    })

    // audio: null,

  },
  jump(e){
    let id = e.currentTarget.dataset.iindex,  //图片的索引
        index = e.currentTarget.dataset.lindex; //列表的索引
    wx.previewImage({
      current: this.data.appoList[index].pic[id], 
      urls: this.data.appoList[index].pic
    })
  },
  video(e){
    let index = e.currentTarget.dataset.index;
    let video = this.data.appoList[index].video;
    
      wx.navigateTo({
        url: './video/video?video='+video,
      })
  },
  money(){
    let self=this;
    app.getBalance(function(res){
      self.setData({
        money: res
      })
    })
  },
  ajax(){

    let self = this; 
    wx.getSetting({
      success(res){
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation']!=true){
          wx.showModal({
            title: '请求授权当前位置',
            content: '需要获取您的地理位置，请确认授权',
            success(res){
              if(res.cancel){
                wx.showToast({
                  title: '拒绝授权',
                  icon:'none',
                  duration:1000
                })  
              }else if(res.confirm){
                wx.openSetting({
                  success(data){
                    
                    if (data.authSetting["scope.userLocation"]==true)
                    {
                      wx.showToast({
                        title: '授权成功',
                        icon:'success',
                        duration:1000
                      })
                      app.util.address(res => {
                        if (res.latitude && res.longitude) {
                          self.setData({
                            latitude: res.latitude,
                            longitude: res.longitude
                          })
                          self.location(res)
                        }
                      })
                    }else{
                      wx.showToast({
                        title: '授权失败',
                        icon:'none',
                        duration:1000
                      })
                    }
                  }
                })    
              }
            }
          })
        } else if (res.authSetting["scope.userLocation"]==undefined)
        {
          app.util.address(res => {
            if (res.latitude && res.longitude) {
              self.setData({
                latitude: res.latitude,
                longitude: res.longitude
              })
              self.location(res)

            }

          })
        }else{
          
          app.util.address(res => {
            if (res.latitude && res.longitude) {
              self.setData({
                latitude: res.latitude,
                longitude: res.longitude
              })
              self.location(res)
            }
          })
        }
      }
    })
       self.index();

   
  },
  index(){
    let self=this;
    app.util.ajax({ url: 'api/index/index', method: 'POST', data: { latitude: self.data.latitude, longitude: self.data.longitude } }, res => {
      this.setData({
        bannerList: res.data.data.banner_list,
        appoList: res.data.data.appointment_list,
        navList: res.data.data.nav_list,
      })

    })

  },
  onLoad: function (option) {
    
    if (decodeURIComponent(option.scene)>1){
      app.globalData.tk_id = decodeURIComponent(option.scene);
    }
    let self=this;
      self.ajax();
 
  },
  location(data){
    let self=this;
    let myAmapFun = new amapFile.AMapWX({ key: markersData.key });
    myAmapFun.getRegeo({
      location: '' + data.longitude + ',' + data.latitude + '',//location的格式为'经度,纬度'
      success: function (data) {
       
        let city = data[0].regeocodeData.addressComponent.city,
          dis = data[0].regeocodeData.addressComponent.district;
        self.setData({
          weizhi: city + dis
        });
      },
      
    });
  },
  onPullDownRefresh(){
    let self=this;
    self.index();
  },
  // onShareAppMessage(res) {
  //   return {
  //     title: '',
  //     path: '/pages/index/index',
  //     success: function (res) {
  //     },
  //     fail: function (res) {
        
  //     }
  //   }
  // },

  //录音
  long(){
    let self = this;
    wx.startRecord({
      success(res){
        let tempFilePath = res.tempFilePath;
        
        let url = 'http://vop.baidu.com/server_api',
          Appid = '14838481',
          Apikey = 'aWT6mPbpASqR75WG1wrKQsPG',
          Secretkey = '8gZKF95m1UG4jMcEWNVGOhmat3xqHckm',
          pid = '1536';
        wx.request({
          url: url,
          data: {
            dev_pid: pid,
            APP_ID: Appid,
            API_KEY: Apikey,
            SECRET_KEY: Secretkey,
            file_get_contents: tempFilePath,
          },
          success: function (res) {
            console.log(res);
          }
        })
      }
    })
    setTimeout(function(){
      wx.stopRecord();
    },10000)
  },
  concat(){
    app.showServiceTips()
  },
  openLocation(){
    let self=this;
    wx.chooseLocation({
      success(res){
        let address=res.name;
        self.setData({
          weizhi:address
        })
      }
    })
  }
})
